# 1.导入库
import numpy as np
import pandas as pd
from sklearn.preprocessing import StandardScaler,MinMaxScaler,MaxAbsScaler

# 2.准备一个两行三列的矩阵
X = [[1,2,3],
     [2,3,4]]

X = np.array(X)
print(X)                 # 查看X的值

ss = StandardScaler()
ss.fit(X)
print(ss.transform(X))   # 利用StandardScaler库对X进行缩放

mms = MinMaxScaler()
mms.fit(X)
print(mms.transform(X))  # 利用MinMaxScalar库对X进行缩放

mas = MaxAbsScaler()
mas.fit(X)
print(mas.transform(X))  # 利用MaxAbsScalar库对X进行缩放


# 手写StandardScalar库的原理
def StandardScalarSelf(X):
     X = np.array(X)
     X_mean = np.mean(X, axis = 0)
     X_std = np.std(X, axis = 0)
     return (X - X_mean) / X_std

# 手写MinMaxScalar库的原理
def MinMaxScalarSelf(X,feature_range=(0, 1)):
     X = np.array(X)
     min, max = feature_range
     X_std = (X - X.min(axis=0)) / (X.max(axis=0) - X.min(axis=0))
     X_scaled = X_std * (max - min) + min
     return X_scaled

# 手写MaxAbsScalar库的原理
def MaxAbsScalarSelf(X):
    X = np.array(X)
    max_abs = np.abs(X).max(axis=0)
    X_scaled_manual = X / max_abs
    return X_scaled_manual

# 主函数调用
if __name__ == "__main__":
     X = [[1,2,3],
          [2,3,4]]
     print(StandardScalarSelf(X))       # 输出
     print(MinMaxScalarSelf(X))         # 输出
     print(MaxAbsScalarSelf(X))         # 输出 



